<?php

/**
 *      52zll.top
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_dzapp_zhibo_user extends discuz_table {

	public function __construct() {
		$this->_table = 'dzapp_zhibo_user';
		$this->_pk    = 'id';

		parent::__construct();
	}

	public function fetch_by_openid($openid) {
		return DB::fetch_first('SELECT * FROM %t WHERE openid=%s', array($this->_table, $openid));
	}

	public function fetch_by_openid_appid($openid, $appid) {
		return DB::fetch_first('SELECT * FROM %t WHERE openid=%s AND appid=%s', array($this->_table, $openid, $appid));
	}

	public function fetch_by_uid_appid($uid, $appid) {
		return DB::fetch_first('SELECT * FROM %t WHERE uid=%d AND appid=%s', array($this->_table, $uid, $appid));
	}

	public function fetch_by_uid($uid) {
		return DB::fetch_first('SELECT * FROM %t WHERE uid=%d', array($this->_table, $uid));
	}

	public function update_by_openid($openid, $data) {
		return DB::update($this->_table, $data, DB::field('openid', $openid));
	}

	public function update_by_openid_appid($openid, $appid, $data) {
		return DB::update($this->_table, $data, DB::field('openid', $openid).' AND '.DB::field('appid', $appid));
	}

	public function update_by_uid_appid($uid, $appid, $data) {
		return DB::update($this->_table, $data, DB::field('uid', $uid).' AND '.DB::field('appid', $appid));
	}

	public function fetch_all_by_appid_uid($appid, $uid) {
		return DB::fetch_all('SELECT * FROM %t WHERE appid=%s AND uid IN (%n)', array($this->_table, $appid, $uid), 'uid');
	}

	public function count_by_search($param) {
		return DB::result_first('SELECT COUNT(*) FROM %t %i', array($this->_table, $this->wheresql($param)));
	}

	public function fetch_all_by_search($param, $start = 0, $limit = 0, $order = 'firsttime', $sort = 'DESC') {
		$ordersql =  $order ? ' ORDER BY '.DB::order($order, $sort) : '';
		return DB::fetch_all('SELECT * FROM %t %i %i '.DB::limit($start, $limit), array($this->_table, $this->wheresql($param), $ordersql));
	}

	public function wheresql($param) {
		foreach($param as $value) {
			if($value[1]) $wherearr[] = DB::field($value[0], is_array($value[1]) ? $value[1] : $value[3].$value[1].$value[4], $value[2] ? $value[2] : '=');
		}
		$wheresql = $wherearr ? 'WHERE '.implode(' AND ', $wherearr) : '';
		return $wheresql;
	}
}
//From:www_caogen8_co
?>